from neo4j import GraphDatabase
import pandas as pd
import  numpy as np

from project_setting import NEO4J_URI,NEO4j_AUTH
__global__dict = {}

def get_neo4j_dirver_instance():
    if 'neo4j' not in __global__dict:
        __global__dict['neo4j'] = GraphDatabase.driver(NEO4J_URI, auth=NEO4j_AUTH)
    return __global__dict['neo4j']



def run_neo4j_query(query,driver):
    with driver.session() as session:
        output = []
        neo4j_results = session.run(query)
        for record in neo4j_results:
            output.append(list(record))
    return output


def _update_tx(tx, cypher, **kwargs):
    result = tx.run(cypher, kwargs)
    return result.consume()

def update_info_to_neo4j(driver, update_query,update_dict):
    print("Updating graph...")
    with driver.session() as session:
        result = session.write_transaction(_update_tx, update_query, **update_dict)
        print("...update complete: {}".format(result.counters))


